from django.shortcuts import render
from rest_framework.views import APIView
# from django
from rest_framework.response import Response
from .models import Brand
from django.conf import settings
import os

# Create your views here.
class Upload(APIView):
    def post(self,request):
        #  FILES;获取前端图片
        files=request.FILES.get('file')
        print('>>>>>>>>>>>>',files)
        # files.file 是字节流 name 是名字
        print('>>>>>>>>>>>>',files.name,files.file)
        # 图片写入位置
        file_dir=settings.STATICFILES_DIRS[0]
        print("图片路径前缀》》》",file_dir)
        # 拼接图片路径
        file_name=file_dir+files.name
        print('完整图片路径》》》',file_name)
        # 写入指定文件
        with open(file_name,'wb') as f:
            # 读取图片字节流写入文件
            f.write(files.file.read())
        return Response({
            'code':200,
            'msg':'上传成功',
            'files_path':file_name,
            'files_name':files.name
        })
class Brands(APIView):
    def post(self,request):
        name=request.data.get('name')
        first_name=request.data.get('first_name')
        logo=request.data.get('logo')
        if not all([name,first_name,logo]):
            return Response({'code':200,'msg':'数据不齐全'})
        try:
            Brand.objects.create(name=name,first_name=first_name,logo=logo)
        except Exception as e:
            print(e)
            return Response({'code':204,'msg':'添加失败'})
        return Response({'code':200,'msg':'添加成功'})
    def get(self,request):
        page=int(request.query_params.get('page'))
        pagesize=int(request.query_params.get('pagesize'))
        brand=Brand.objects.all()[(page-1)*pagesize:(page*pagesize)]
        brand_list=[]
        for i in brand:
            brand_list.append({
                'id':i.id,
                'name':i.name,
                'first_name': i.first_name,
                'logo': i.logo,
            })
        return Response({
            'code':200,
            'msg':'获取全部成功',
            'result':brand_list,
            'total':Brand.objects.all().count()
        })
class BrandInfo(APIView):
    def get(self,request,id):
        try:
            brand = Brand.objects.get(id=id)
            print('-------》', brand)
        except Exception as e:
            print(e)
            return Response({'code':204,'msg':'查询失败'})
        return Response({
                'id': brand.id,
                'name': brand.name,
                'first_name': brand.first_name,
                'logo': brand.logo,
            })
    def put(self,request,id):
        name = request.data.get('name')
        first_name = request.data.get('first_name')
        logo = request.data.get('logo')
        try:
            brand=Brand.objects.get(id=id)
            brand.name=name
            brand.first_name=first_name
            brand.logo=logo
            brand.save()
        except Exception as e:
            print(e)
            return Response({'code': 204, 'msg': '修改失败'})
        return Response({'code': 200, 'msg': '修改成功'})
    def delete(self,request,id):
        try:
            brand = Brand.objects.get(id=id).delete()
        except Exception as e:
            print(e)
            return Response({'code':204,'msg':'数据不存在'})
        return Response({'code': 200, 'msg': '删除成功'})



